package cn.iocoder.yudao.module.game.service.otherGame;

import cn.iocoder.yudao.module.game.controller.admin.gameToken.vo.GameTokenSaveReqVO;
import cn.iocoder.yudao.module.game.dal.dataobject.gameToken.GameTokenDO;
import cn.iocoder.yudao.module.game.service.gameToken.GameTokenService;
import cn.iocoder.yudao.module.game.utils.AESEncrypt;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import net.sf.json.JSONObject;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.net.URLEncoder;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.error;

/**
 * 游戏列表 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class RPGServiceImpl implements RPGService {


    // 测试
//    private String operator_token = "98d70a4984d7cd7acc3a39412ff4cc3e";
//    private String secret_key = "e97e5527edd418ca75b7f0d7887d9697";

    // 正式
    private String operator_token = "f44fecbe3549691fb2abac3009885581";
    private String secret_key = "d7b47d0029440bba156f3e49634a507e";

    private String url = "https://www.pgpublic.com/pggame/index/createuser/";

    @Resource
    private GameTokenService gameTokenService;

    @Override
    public String login(Map<String, Object> params) throws Exception {
        System.out.println("userid: " + params.get("userId"));
        String queryParams = "";

        queryParams += "?ot=" + operator_token;
        queryParams += "&btt=1";
        queryParams += "&iwk=1";
        queryParams += "&oc=0";
        queryParams += "&l=p";

        queryParams += "&ops=" + params.get("token");
        System.out.println("queryParams: " + queryParams);
        return url + params.get("gameId") + "/index.html" + queryParams;
    }

    @Override
    public String getToken(String userId) throws Exception {
        GameTokenDO gameTokenDO = gameTokenService.getToken(new LambdaQueryWrapper<GameTokenDO>()
                .eq(GameTokenDO::getMemberId, userId)
                .eq(GameTokenDO::getGameType, "RPG"));
        if (gameTokenDO != null) {
            return gameTokenDO.getToken();
        }

        System.out.println("getToken=========== ");
        String tokenParams = "";
        tokenParams += "?ot=" + operator_token;
        tokenParams += "&sk=" + secret_key;
        tokenParams += "&userId=" + userId;
        tokenParams += "&btt=1";

        String token = "";
        token = AESEncrypt.encrypt(tokenParams, secret_key.substring(0, 16), operator_token.substring(0, 16));
        System.out.println("token: " + token);
        token = URLEncoder.encode(token, "UTF-8");

        GameTokenSaveReqVO createReqVO = new GameTokenSaveReqVO();
        createReqVO.setMemberId(Long.valueOf(userId));
        createReqVO.setToken(token);
        createReqVO.setGameType("RPG");
        gameTokenService.createToken(createReqVO);

        return token;
    }

    @Override
    public GameTokenDO checkToken(Map<String, Object> params) throws Exception {
//        if (!params.get("operator_token").equals(operator_token)) {
//            return null;
//        }
//        if (!params.get("secret_key").equals(secret_key)) {
//            return null;
//        }
        GameTokenDO gameTokenDO = gameTokenService.getToken(new LambdaQueryWrapper<GameTokenDO>()
                .eq(GameTokenDO::getToken, params.get("operator_player_session").toString())
                .eq(GameTokenDO::getGameType, "RPG"));

        if (gameTokenDO == null) {
            return null;
        }
        return gameTokenDO;
    }
}